<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use DB;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ZhuanlanController extends Controller
{
	/**
     * 专栏页面
     *
     * @return data
     */
	public function getIndex()
	{
        // $res = DB::table('column')
        //     ->where('examine','=','1')
        //     ->where('status','=','0')
        //     ->count();
        // $res = ceil($res/8);
        // dd($res);
		$data = DB::table('column')
            ->where('examine','=','1')
            ->where('status','=','0')
            // select * from 'column' orderBy rand() limit 5
            // ->orderBy('rand()')
            // ->take(8)
            // ->limit(8)
            ->paginate(8);
            
		//分配模板
		return view('/home.zhuanlan.index',['data'=>$data]);
		
	}

	/**
     * ajax查询
     *
     * @return data
     */
	public function getChaxun()
	{
		 //查询数据
		$data = DB::table('column')
			->leftJoin('userdetails','column.uid','=','userdetails.uid')
			->select('column.*','userdetails.pic')
			->where('examine','=','1')
			->get();
		return $data;
	}

	/**
     * 申请专栏页面
     *
     * @return data
     */

	public function getShenqing()
	{
		return view('/home.zhuanlan.shenqing');
	}

    /**
     * Ajax查询专栏名
     *
     * @return data
     */
    public function getZlcx(Request $request)
    {
        //查询
        $data = DB::table('column')
            ->where('cname',$request->input('cname'))
            ->get();
        if($data){
            return 1;
        }else{
            return 2;
        }
    }

	/**
     * Ajax查询话题
     *
     * @return data
     */
	public function getSousuo(Request $request)
	{
		//查询
		$data = DB::table('topic')
			->where('title','like','%'.$request->input('topic').'%')
			->get();
			return $data;
	}

	/**
     * 执行申请专栏的提交动作
     *
     * @return res
     */
	public function getColumn(Request $request)
	{
        @session_start();
        $id = $_SESSION['id'];
        
		//获取提交的数据
		$data = $request->except('topic');
        
		//将数据插入数据库
		$res = DB::table('column')
            ->insert([
                ['cname'=>$data['cname'],'tid'=>$data['tid'],'content'=>$data['content'],'uid'=>$id]
                ]);
		
		//判断是否插入成功
		if($res)
		{
            echo "<script>alert('提交成功');window.location.href='{$_SERVER['HTTP_REFERER']}'</script>";
        }else{
            echo "<script>alert('提交失败');window.location.href='{$_SERVER['HTTP_REFERER']}'</script>";
            
		}
	}

	/**
     * 专栏详情
     *
     * @return data  dete
     */
	public function getZhuanlan(Request $request)
	{
		//获取专栏id
		$id = $request->all();
		//查询主数据
		$dete = DB::table('column')
			->leftJoin('topic','column.tid','=','topic.id')
			->select('column.*','topic.title as ttitle')
			->where('column.id',$id)
			->get();

		//查询文章数据
		$data = DB::table('columnarticle')
			->leftJoin('column','column.id','=','columnarticle.coid')
			->leftJoin('topic','column.tid','=','topic.id')
			->leftJoin('user','columnarticle.uid','=','user.id')
			->select('column.*','columnarticle.*','topic.title as ttitle','user.name as uname','user.id as uid')
			->where('column.id',$id)
			->where('cstatus','=','0')
			->get();
			
		//分配模板
		return view('/home/zhuanlan/zhuanlan',['data'=>$data,'dete'=>$dete,'id'=>$id]);
	}

	/**
     * 写文章页面
     *
     * @return view
     */
    public function getZhuanlanarticle()
    {
        return view('/home.zhuanlan.zhuanlanarticle');
    }

    /**
     * 执行上传
     *
     * @return res
     */
    public function postAdd(Request $request)
    {
        session_start();
        $id = $_SESSION['id'];
        
        //获取文章内容
        $data = $request->except(['_token','topic']);
        //获取coid
        $coid = $data['id'];
        //查询tid
        $tid = DB::table('column')
        	->select('column.tid')
        	->where('id',$coid)
        	->first();	
        $tid = $tid->tid;
        //获取时间
        $date = time();
        //执行添加文章操作
        $res = DB::table('columnarticle')
            ->insert([
                ['uid'=>$id,'coid'=>$data['id'],'tid'=>$tid,'ctitle'=>$data['ctitle'],'ccontent'=>$data['ccontent'],'time'=>$date]
                ]);
        //判断是否发表成功
        if($res)
        {
            echo "<script>alert('发布成功');window.location.href='{$_SERVER['HTTP_REFERER']}'</script>";
        }else{
            echo "<script>alert('发布失败');window.location.href='{$_SERVER['HTTP_REFERER']}'</script>";
        }
    }
    /**
     * 我的专栏
     *
     * @return data
     */
    public function getWdzl(Request $request)
    {
    	//获取用户id
        $id = $request->all();
       
        //查询专栏数据
    	$dete = DB::table('column')
			->Join('userdetails','column.uid','=','userdetails.uid')
			->Join('topic','column.tid','=','topic.id')
			->select('column.*','topic.title as ttitle')
			->where('column.id',$id)
			->get();

		//查询文章数据
		$data = DB::table('columnarticle')
			->leftJoin('column','column.id','=','columnarticle.coid')
			->leftJoin('userdetails','column.uid','=','userdetails.uid')
			->leftJoin('topic','column.tid','=','topic.id')
			->select('column.*','columnarticle.*','topic.title as ttitle')
			->where('column.id',$id)
			->where('cstatus','=','0')
			->orderBy('time','desc')
			->get();
    	//分配模板
    	return view('/home/zhuanlan/wdzl',['data'=>$data,'dete'=>$dete,'id'=>$id]);
    }

     /**
     * 修改专栏头像
     *
     * @return data
     */
     public function getGrzl(Request $request)
     {
     	//获取id
     	$id = $request->all();
     	//查询数据
     	$data = DB::table('column')
     		->where('id',$id)
     		->get();

     	return view('/home/zhuanlan/grzl',['data'=>$data]);
     }

      /**
     * 执行修改操作
     *
     * @return res
     */
      public function postUpdate(Request $request)
      {
      	//获取所有数据
      	$data = $request->except(['_token']);
      	$id = $data['id'];
      	//判断是否有图片上传
      	if($request->hasFile('zlpic')){
      		//随机文件名
      		$name = md5(time()+rand(1,99999));
      		//获取文件后缀名
      		$suffix = $request->file('zlpic')->getClientOriginalExtension();
      		$arr = ['png','jpeg','gif','jpg'];
      		if(!in_array($suffix,$arr)){
      			return back()->with('error','上传文件格式不正确');
      		}
      		$request->file('zlpic')->move('./uploads/',$name.'.'.$suffix);
      		//返回路径
      		$str = '/uploads/'.$name.'.'.$suffix;
      		//将路径存入数据库
      		$res = DB::table('column')->where('id',$id)->update(['zlpic'=>$str]);
      		//判断是否发表成功
            if($res){
                    echo "<script>alert('修改成功');window.location.href='{$_SERVER['HTTP_REFERER']}'</script>";
                }else{
                    echo "<script>alert('修改失败');window.location.href='{$_SERVER['HTTP_REFERER']}'</script>";
                }
      	}else{
            echo "<script>alert('请您选择要修改的头像');window.location.href='{$_SERVER['HTTP_REFERER']}'</script>";
            
            die;
        }

      }

    /**
     * ajax查询专栏名操作
     *
     * @return 专栏名已存在返回1  不存在返回2
     */
    public function getZlmcx(Request $request)
    {
        //获取专栏名
        $cname = $request->input('cname');
        //查询数据库里是否有同名的
        $data = DB::table('column')->where('cname',$cname)->get();
        //判断是否有同名
        if($data){
            //同名返回1
            return 1;
        }else{
            // 不同名返回2
            return 2;
        }
    }

    /**
     * ajax修改专栏名操作
     *
     * @return res
     */
    public function getZlmxg(Request $request)
    {
        //获取专栏名
        $cname = $request->input('cname');
        //获取专栏id
        $id = $request->input('id');
        //执行修改操作
        $data = DB::table('column')->where('id',$id)->update(['cname'=>$cname]);
        
        //判断是否修改成功
        if($data){
            //成功返回1
            return 1;
        }else{
            // 不成功返回2
            return 2;
        }
    }

     /**
     * 执行点赞操作
     *
     * @return res
     */
    public function getDianzan(Request $request)
    {
        //开启session
        session_start();
        //获取用户id
        $id = $_SESSION['id'];
        //获取当前时间戳
        $time = time();
        //获取要点赞的文章id
        $wzid = $request->input('wzid');
        //查询是否已经点赞
        $rees = DB::table('guanxiwz')->where('wzid',$wzid)->where('uuuid',$id)->get();
        //如果已点赞 die
        if($rees){
            die;
        }else{
            //将id time wzid插入guanxiwz表中
            $ress = DB::table('guanxiwz')->insert([
                ['uuuid'=>$id,'wztime'=>$time,'wzid'=>$wzid]
                ]);
            //获取点赞数
            $pass = $request->input('pass');
            //给点赞数加一
            $newpass = $pass+1;
            //修改点赞数
            $res = DB::table('columnarticle')->where('id',$wzid)->update(['pass'=>$newpass]);
            // 判断是否执行成功
            if($res){
                echo $newpass;
            }else{
                echo 0;
            }
        }
    }
}
